package com.eascs.crm.nsEditDetail.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.CustomerAssetInfo;
import com.eascs.crm.nsEditDetail.entity.CustomerAssetInfoHis;
import com.eascs.crm.nsEditDetail.form.CustomerAssetInfoForm;
import com.eascs.crm.nsEditDetail.service.CustomerAssetInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
* @author bao.pan 2017年7月13日 下午4:12:21
*/
@Controller
@RequestMapping("/rasset")
public class CustomerAssetInfoRestController extends BaseRestController{
	private final Logger logger = LoggerFactory.getLogger(CustomerAssetInfoRestController.class);
	@Autowired
	private CustomerAssetInfoService customerAssetInfoService;

	// 修改
	@RequestMapping(value = "/assetedit")
	public HtmlRestResult editIndex(String loanapplyNo) {
		CustomerAssetInfo assetInfo=customerAssetInfoService.queryById(loanapplyNo);
		ViewContext.put("assetInfo", assetInfo);
		ViewContext.put("loanapplyNo", loanapplyNo);
		return  HtmlRestResult.popHtmlFragment("nsEditDetail/customer_asset_info_edit");
	}
	@RequestMapping(value = "/updateAssetInfo")
	public BaseRestResult<String> saveOrupdateCustomerAssetInfo(CustomerAssetInfoForm cbiForm) {
		BaseRestResult<String> result = null;
//		if("Y".equals(cbiForm.getIsLegalDisputes())&&StringUtil.isEmpty(cbiForm.getLegalDisputesInfo())){
//			result = BaseRestResult.warn("请填写法律纠纷的原因");
//		}else 
		String type="update";
		User user = UserHolder.getUser();
		if(StringUtil.isEmpty(cbiForm.getCarNetValue())){
			result = BaseRestResult.warn("请填写车产净价值");
		}else if(StringUtil.isEmpty(cbiForm.getHouseValue())){
			result = BaseRestResult.warn("请填写房产价值");
		}else{
			CustomerAssetInfo assetInfo=customerAssetInfoService.queryById(cbiForm.getLoanapplyNo());
			if (assetInfo==null) {
				assetInfo=new CustomerAssetInfo();
				assetInfo.setCreateId(user.getNo());
				assetInfo.setCreator(user.getCname());
				assetInfo.setCreateDate(new Date());
				assetInfo.setLoanapplyNo(cbiForm.getLoanapplyNo());
				type="add";
			}
			assetInfo.setBankLoanItems(cbiForm.getBankLoanItems());
			assetInfo.setBorrowTotal(cbiForm.getBorrowTotal());
			assetInfo.setCreditNum(cbiForm.getCreditNum());
			assetInfo.setEstateLoanOver(cbiForm.getEstateLoanOver());
			assetInfo.setEstateMonthlyAmount(cbiForm.getEstateMonthlyAmount());
			assetInfo.setEstateMortgageTerm(cbiForm.getEstateMortgageTerm());
//			assetInfo.setIsLegalDisputes(cbiForm.getIsLegalDisputes());
			assetInfo.setLegalDisputesInfo(cbiForm.getLegalDisputesInfo());
			assetInfo.setLoanMonthlyRepayment(cbiForm.getLoanMonthlyRepayment());
			assetInfo.setLoanOver(cbiForm.getLoanOver());
			assetInfo.setLoanTerm(cbiForm.getLoanTerm());
			assetInfo.setLoanTotal(cbiForm.getLoanTotal());
			assetInfo.setMortgageNum(cbiForm.getMortgageNum());
			assetInfo.setOtherAssetsValue(cbiForm.getOtherAssetsValue());
			assetInfo.setOtherLoan(cbiForm.getOtherLoan());
			assetInfo.setOverdraft(cbiForm.getOverdraft());
			assetInfo.setOwnEstateNum(cbiForm.getOwnEstateNum());
			assetInfo.setOwnVehiclesNum(cbiForm.getOwnVehiclesNum());
			assetInfo.setRedNum(cbiForm.getRedNum());
			assetInfo.setUnsecuredNum(cbiForm.getUnsecuredNum());
			assetInfo.setVehicleMonthlyAmount(cbiForm.getVehicleMonthlyAmount());
			assetInfo.setVehicleMortgageOver(cbiForm.getVehicleMortgageOver());
			assetInfo.setVehicleMortgageTerm(cbiForm.getVehicleMortgageTerm());
			assetInfo.setVehicleMortgageNum(cbiForm.getVehicleMortgageNum());
			assetInfo.setOtherInvest(cbiForm.getOtherInvest());
			assetInfo.setCarNetValue(cbiForm.getCarNetValue());
			assetInfo.setHouseValue(cbiForm.getHouseValue());
			assetInfo.setRemark(cbiForm.getRemark());
			assetInfo.setModifyId(user.getNo());
			assetInfo.setModifier(user.getCname());
			assetInfo.setModifyDate(new Date());
			
			CustomerAssetInfoHis his=new CustomerAssetInfoHis();
			BeanUtils.copyProperties(assetInfo, his);
			boolean results;
			if ("update".equals(type)) {
				 results = customerAssetInfoService.update(assetInfo,his);
			}else {
				results = customerAssetInfoService.add(assetInfo,his);
			}

			if (results) {
				result = BaseRestResult.info("添加成功").andRefresh();
			} else {
				result = BaseRestResult.error("操作失败");
			}
		}
		
		return result;
	}
}
